<template>
  <div class="region">
    <h1>医院</h1>
    <div class="content">
      <div class="left">等级:</div>
      <ul class="hospital">
        <li :class="{ active: activeRef === '' }" @click="changeLevelVal('')">
          全部
        </li>
        <li
          v-for="item in hosLevelList"
          :key="item.id"
          :class="{ active: activeRef === item.value }"
          @click="changeLevelVal(item.value)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>
<script lang="ts" name="Level" setup>
// 获取医院等级字典数据
import { DictData, Result } from "@/types/types.ts";
import { doFindByDictCode } from "@/api/home";
import { DictCodeEnums } from "@/types/enums.ts";
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";

// 控制高亮
let activeRef = ref<string>("");
let hosLevelList = ref<Array<DictData>>([]);

onMounted(() => {
  getLevel();
});

// 获取医院等级字典数据
const getLevel = async () => {
  let result: Result = await doFindByDictCode(DictCodeEnums.HOSPITAL);
  if (result.code === 200) {
    hosLevelList.value = result.data;
  } else {
    ElMessage.error("医院字典无数据");
  }
};
// 选择医院等级
const changeLevelVal = (value: string) => {
  activeRef.value = value;
  emit("sendLevel", value);
};

// 子传父
let emit = defineEmits(["sendLevel"]);
</script>

<style lang="scss" scoped>
.region {
  color: #7f7f7f;
  h1 {
    font-weight: 900;
    margin: 10px 0;
  }
  .content {
    display: flex;
    .left {
      margin-right: 10px;
    }
    .hospital {
      display: flex;
      li {
        margin-right: 10px;
        &.active {
          color: #55a6fe;
        }
      }
      li:hover {
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}
</style>
